*! version 2.0
* 21 August 2013
* NIDS
* Coding employment status

*====================================================================================================================================

* NOTE		

/*

The following variables were used in the original do file, they are listed with their Wave 1 names and their wave names, Wave 2 and Wave 3 as the file
was originally writen using the W1 question numbers they have been converted to wave names.

w1			wave			w2             w3
e1  	->	em1			->	e2         ->  e2
e18		->	em2			->	e23        ->  e23
e28		->	ems			->	e33        ->  e33
e40		->	emc			->	e48        ->  e48
e45		->	emp			->	e53        ->  e53
e52		->	emh			->	e61        ->  e61
e66		->	unemwnt		->	e74        ->  e74
e71_1a	->	unems* 		->	e79*       ->  e79*

*/

*=====================================================================================================================================
* GLOBALS FOR DATA FILES, DO FILES AND VERSION SUFFIXES

global DataIN "\\137.158.104.21\data\Panel Public Release 2014a\Wave 3\Anon"
global DataOUT "C:\Users\01406074\Desktop"

global temp "C:\Users\01406074\Desktop"			// tempfile to hold all the working datasets, all working datasets will
																// be deleted from this folder at the completion of the do file execution.
global VersionIN "W3_Anon_V1.2"
global VersionOUT "Derived"

version 12.0													// version of Stata being used, this is needed for the rename command.

*=====================================================================================================================================
set more off

* ADULT

use "$DataIN\Adult_$VersionIN.dta", clear

cap rename w3_a_* *

gen done_nothing = 1 if unems1 == 11

gen Employed= (em1==1|em2==1|ems==1|emc==1|emp==1|emh==1)
gen Not_economically_active= (Employed==0 & unemwnt==2)
gen Unemployed_discouraged= (Employed==0 & unemwnt==1 & done_nothing == 1)
gen Unemployed_strict= (Employed==0 & unemwnt==1 & done_nothing != 1)

gen state=.
replace state=0 if Not_economically_active==1
replace state=1 if Unemployed_discouraged==1
replace state=2 if Unemployed_strict==1
replace state=3 if Employed==1
replace state=-8 if state == . & outcome == 1

replace state=. if outcome != 1

la def state 0"Not Economically Active" 1"Unemployed_Discouraged" 2"Unemployed_Strict" 3"Employed" -8"Refused"
la val state state

ren state state_fine_a

gen state2=0 if state_fine_a == 0
replace state2=1 if state_fine_a >=1 & state_fine_a <=2
replace state2=2 if state_fine_a ==3
replace state2=-8 if state_fine_a == -8
la def state2 0"Not Economically Active" 1"Unemployed" 2"Employed" -8"Refused"
la val state2 state2

rename state2 state_coarse_a

keep pid w3_hhid state_fine_a  state_coarse_a

sort  pid

save "$temp\1_labour.dta", replace

* END OF USING THE ADULT

*=================================================================================================================================

* PROXY

use "$DataIN\Proxy_$VersionIN.dta", 
rename w3_p_* *
gen state=.
replace state=0 if emp>=5 & emp<=8
replace state=1 if emp==10
replace state=2 if emp==9
replace state=3 if emp>=1 & emp<=4
replace state=-8 if state == . & outcome == 6

replace state=. if outcome != 6
la def state 0"Not Economically Active" 1"Unemployed_Discouraged" 2"Unemployed_Strict" 3"Employed" -8"Refused"
la val state state

ren state state_fine_p

gen state2=0 if state_fine_p == 0
replace state2=1 if state_fine_p >=1 & state_fine_p <=2
replace state2=2 if state_fine_p ==3
replace state2=-8 if state_fine_p == -8

la def state2 0"Not Economically Active" 1"Unemployed" 2"Employed" -8"Refused"
la val state2 state2
ren state2  state_coarse_p

keep pid w3_hhid  state_fine_p  state_coarse_p
sort pid

save "$temp\2_labour.dta", replace

* END OF USING PROXY

*=================================================================================================================================

/*
INDDERIVED
*/

use "$DataIN\indderived_$VersionIN.dta", clear

cap drop w3_empl_stat_narrow w3_empl_stat_broad

merge 1:1 pid w3_hhid using "$temp\1_labour.dta"
drop _merge 

sort pid
merge 1:1 pid w3_hhid using "$temp\2_labour.dta"
drop _merge


gen state_fine_ap = .
replace state_fine_ap = state_fine_a if state_fine_a !=.
replace state_fine_ap = state_fine_p if state_fine_p !=.

gen state_coarse_ap = .
replace state_coarse_ap = state_coarse_a if state_coarse_a !=.
replace state_coarse_ap = state_coarse_p if state_coarse_p !=.


la val state_fine_ap state_fine_ap

la val state_coarse_ap state_coarse_ap


ren state_coarse_ap w3_empl_stat_broad
ren state_fine_ap   w3_empl_stat_narrow

lab var w3_empl_stat_broad "Employment status - Broad definition"
lab var w3_empl_stat_narrow "Employment status - Narrow definition"

cap drop state_fine_a state_coarse_a state_fine_p state_coarse_p

duplicates drop pid, force
save "$DataOUT\indderived_EMPLOYMENT_STATUS.dta", replace

*------------------------------------------------------------------------------------------------------------------------------------

* ERASING THE TEMP FILES

erase "$temp\1_labour.dta"
erase "$temp\2_labour.dta"

* end of do file
*=====================================================================================================================================
